<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2024/4/25
  Time: 19:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>教师列表</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <!--顶部：搜索form表单-->
    <form class="layui-form">
        姓名：
        <div class="layui-inline layui-input-wrap">
            <input type="text" name="name" lay-affix="clear" class="layui-input">
        </div>
        所在系：
        <div class="layui-inline layui-input-wrap">
            <select id="xiId" name="xiId" lay-filter="xiId">
                <option value="">请选择</option>
            </select>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">清空</button>
        </div>
    </form>

    <!--页内容-->
    <table class="layui-hide" id="test" lay-filter="test"></table>
    <%--左上角：头部工具栏事件---批量删除、添加--%>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>
    </script>

    <%--右侧：触发单元格工具事件---单个元素的删除、编辑--%>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
        </div>
    </script>

    <%--table中展示status的templet--%><%--这是JS代码--%>
    <script type="text/html" id="statusTemplet">

        <%--用按钮来表示上架或下架--%>
        <input type="checkbox" name="status" value="{{= d.id }}" title="工作|请假" lay-skin="switch"
               lay-filter="statusFilter" {{= d.status == 1 ? "checked" : "" }}>

    </script>


    <script>
        /*layui库中表单模块form，表格模块table,日期选择器模块laydate*/
        layui.use(['form','table','laydate'],function (){
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;


            form.on('switch(statusFilter)',function (obj) {
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 1 : 0;
                $.post(
                    '/xiTeacher?method=updateStatus',
                    {'id':id,'status':status},
                    function (result) {
                        if (result.code == 0){
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });

            /*//日期
            laydate.render({
                elem: '#beginCreateTime',
                type: 'datetime'
            });
            laydate.render({
                elem: '#endCreateTime',
                type: 'datetime'
            });*/

            $.post(
                '/xi?method=selectAll',
                function (result) {
                    if (result.code == 0){
                        $(result.data).each(function () {
                            $('#xiId').append('<option value="'+this.id+'">'+this.name+'</option>');
                        })
                        //局部变动，渲染一下
                        form.render('select');
                    }
                },
                'json'
            );

            // 创建渲染实例
            table.render({
                elem:'#test', /*页内容的id*/
                id:'tableId',
                url:'/xiTeacher?method=selectByPage',
                toolbar: '#toolbarDemo',
                cols:[[
                    {type:'checkbox',fixed: 'left'}, /*左侧fixed,复选框checkbox*/
                    {field:'id',title:'ID',sort:true,width: 100},/*sort用于排序*/
                    {field: 'name',title: '姓名'},
                    {field: 'gender',title: '性别'},
                    {field: 'title',title: '职称'},
                    {field: 'status',title: '在校状态',templet:'#statusTemplet',width: 100},
                    {field: 'xiName',title: '系名'},
                    {field: 'createTime',title: '创建时间',width: 170},
                    {field: 'updateTime',title: '更新时间',width: 170},
                    {fixed:'right',title: '操作',width:150,toolbar:'#barDemo'}
                ]],
                page: true
            });

            //搜索提交
            form.on('submit(submitSearch)',function (data) {
                //获得表单字段
                var field = data.field;
                //执行搜索重载
                table.reload('tableId',{
                    page: {
                        //重新从第1页开始
                        curr:1
                    },
                    //搜索的字段
                    where:field
                    //http://localhost:8080/student?method=selectByPage&page=1&limit=10&name=zhansgsan&address=5&time=2014
                });
                //阻止默认 form跳转
                return false;
            });

            //头部工具栏事件---批量删除、添加
            table.on('toolbar(test)',function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);

                switch (obj.event){
                    //----------------------添加------------------------------
                    case 'add':
                        var index = layer.open({
                            type:2,
                            title:'添加数据',
                            area:['90%','90%'],// 宽高
                            content: '/page/xiteacher/add'
                        });
                        break;
                    //-------------------------删除------------------------------
                    case 'deleteAll':
                        var data = checkStatus.data;
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        layer.confirm(
                            '您确认要删除吗？',
                            {icon:3},
                            function (index) {
                                $.post(
                                    '/xiTeacher?method=deleteAll',
                                    {'ids':ids},
                                    function (result) {
                                        console.log(result);
                                        if (result.code == 0){
                                            mylayer.okMsg(result.msg);
                                            //删除成功之后刷新表格，展示最新数据
                                            table.reload('tableId');
                                        } else {
                                            mylayer.errorMsg(result.msg);
                                        }
                                    },
                                    'json'
                                );
                            }
                        );
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.alert('自定义工具栏图标按钮');
                        break;
                }
            });

            //触发单元格工具事件---单个元素的删除、编辑
            table.on('tool(test)',function (obj) {
                //获得当前行数据
                var data = obj.data;
                console.log(obj);
                console.log(obj.data);
                if (obj.event === 'delete'){
                    layer.confirm(
                        '您确认要删除吗？',
                        {icon:3},
                        function (index) {
                            $.post(
                                '/xiTeacher?method=deleteById',
                                {'id':data.id},
                                function (result) {
                                    console.log(result);
                                    if (result.code == 0){
                                        mylayer.okMsg(result.msg);
                                        //删除成功之后刷新表格，展示最新数据
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                        }
                    );
                    //---------------------编辑-------------------------------
                } else if (obj.event === 'update'){
                    var index = layer.open({
                        type:2,
                        title:'编辑数据',
                        area:['90%','90%'],// 宽高
                        content:'/page/xiteacher/update?id=' + data.id,
                    });
                } else if (obj.event === 'detail'){
                    layer.open({
                        type:1,
                        title:data.name,
                        area:['90%','90%'],//宽高
                        content:data.content,
                    })
                }
            });
        });
    </script>
</body>
</html>
